import {
  Base64
} from '@/utils/xchat/base64';
import {
  flvCommon
} from '@/utils';
import { chatDataHandle } from './chatDataHandle';
import User from '@/utils/xMessager/User';

export function MSG_LOGIN_ACK(param, { xmessager, chatDisplay }) {
  if (param.loginack === 0) {
    xmessager.logined = false;
    switch (param.reason) {
    case 2:
      chatDisplay.common_insert('<span class="warning">服务器人数满了！</span>', 1);
      break;
    case 3:
      chatDisplay.common_insert('<span class="warning">房间人数满了！</span>', 1);
      break;
    case 5:
      chatDisplay.common_insert('<font class="warning">账号已经在其他页面登录本房间！</font>', 1);
      break;
    }
  }
}
export function MSG_REENTER(param, { xmessager, player, layer }) {
  xmessager.logined = false;
	let classname = !window.is_official ? 'layer-loginout-xchat' : 'layer-loginout-xchat layer-loginout-xchat-no-official'
  layer.open({
    type: 1,
    title: false,
    closeBtn: 0,
    shadeClose: false,
    className: classname,
    content: '<div class="reason"><i></i>账号已从别处登录</div>'
  });
  if (player) {
    player.destroy();
  }
}
export function MSG_SESSION_PARAM(param, { chatDisplay, xmessager }) {
  xmessager.logined = true;
  chatDisplay.common_insert('聊天服务器连接成功!', 1);
}
export function MSG_SOMEBODY_IN(appdata, { xmessager, chatDisplay, roompara }) {
  console.log(`in-out-hint:${appdata.in_out_hint}`);
  if (appdata.in_out_hint) {
    appdata.type = 1;
    if (!appdata.from_nickname && appdata.nickname) {
      appdata.from_nickname = appdata.nickname;
    }
    if (!appdata.to_uid && appdata.touid) {
      appdata.to_uid = appdata.touid;
    }
    if (xmessager.level > 100) {
      chatDisplay.disinout(appdata, roompara);
    }
  }
}
export function MSG_SOMEBODY_OUT(param, appdata, { xmessager, chatDisplay, roompara }) {
  if (param.in_out_hint) {
    appdata.type = 0;
    if (!appdata.from_nickname && appdata.nickname) {
      appdata.from_nickname = appdata.nickname;
    }
    if (!appdata.to_uid && appdata.touid) {
      appdata.to_uid = appdata.touid;
    }
    if (xmessager.level > 100) {
      chatDisplay.disinout(appdata, roompara);
    }
  }
}
export function MSG_ROOMHOT(param, store) {
  store.commit('SET_ALLCOUNT', param.roomhot);
}
export function MSG_SOMEBODY_CHATENABLE(param, { xmessager, chatDisplay, chat }) {
  if (param.uid == xmessager.uid) {
    if (param.chatenable) {
      chatDisplay.common_insert('<span class="warning">你的禁止发言已经解除!</span>', 1);
      xmessager.chatdisable = 0;
    } else {
      chatDisplay.common_insert('<span class="warning">你的禁止发言已经解除!</span>', 1);
      xmessager.chatdisable = 0;
    }
  } else {
    const u = chat.getUser(param.uid);
    if (u) {
      if (!param.chatenable) {
        const s = `<span class="warning">【${u.appdata.nickname}】被被管理员禁止发言15分钟!</span>`;
        chatDisplay.common_insert(s, 1);
      }
    }
  }
}
export function MSG_KICKOUT(param, { xmessager, chatDisplay, chat }) {
  if (param.uid == xmessager.uid) {
    chatDisplay.common_insert('<span class="warning">你被管理员踢出了！1小时内不允许进入本房间！</span>', 1);
    setTimeout(() => {
      location.reload(true);
    }, 5000);
  } else {
    const u = chat.getUser(param.uid);
    if (u) {
      const s = `<span class="warning">【${u.appdata.nickname}(${u.appdata.uid})】被管理员踢出房间了。1小时内不允许进入本房间。</span>`;
      chatDisplay.common_insert(s, 1);
    }
  }
}
export function MSG_MESSAGE(chatdata, { chatDisplay, roompara, xmessager, store, chat, header, mode }) {
  if (!chatdata.data.from_nickname && chatdata.data.nickname) {
    chatdata.data.from_nickname = chatdata.data.nickname;
  }
  if (!chatdata.data.to_uid && chatdata.data.touid) {
    chatdata.data.to_uid = chatdata.data.touid;
  }
  chatDataHandle(chatdata, { chatDisplay, roompara, xmessager, store, chat, header, mode });
}
let loadingTimer = null;
export function MSG_TOKEN_INFO(param, {
  chat,
  player,
  xmessager,
  store,
  chatDisplay,
	vm
}) {
  if (xmessager.level < 500 && store.getters.roompara.is_pay_room == 1 && (xmessager.expire_time && (Math.floor(new Date().getTime() / 1000) > xmessager.expire_time))) {
    location.reload();
  }
  if (param.tokenid === 0) {
    if (param.tokenstatus === 1) {
      const tokenop_oper_appdata = JSON.parse(Base64.decode(param.tokenop_oper_appdata));
      tokenop_oper_appdata.nickname = Base64.decode(tokenop_oper_appdata.nickname_b64);
      if (store.getters.roompara && store.getters.roompara.parent_room_id != 0) {
        const obj = {};
        obj.level = 500;
        obj.uid = tokenop_oper_appdata.uid;
        obj.appdata = tokenop_oper_appdata;
        chat.userIn(new User(obj));
				vm.setPrivateChatArray(chat.private_chat_array)
      }
      // 设置上麦时间以供自动切换使用
      const mictime = new Date().getTime();
      if (player) {
        player.mictime = mictime;
      }
      const i = 0;
      const status = () => {
        store.commit('SET_MICLOADING', false);
        const url_data = JSON.parse(Base64.decode(param.tokenop_comment));
        chat.emitter.trigger(chat.chatEvents.SHANGMAI, { url_data, mictime });
      };
      const roomchange = function () {
        if (store.getters.roomChange) {
          if (/(Android)/i.test(navigator.userAgent) && !flvCommon.flvSupport) {
            store.commit('SET_ROOMLIVE', true);
            status();
          } else {
            store.commit('SET_ROOMLIVE', true);
            status();
          }
        }
      };
      if (!player) {
        if (store.getters.roompara && 'login_alert_time' in store.getters.roompara) {
          if ((store.getters.roompara.login_alert_time) || xmessager.level >= 100) {
            roomchange();
          }
        } else {
          roomchange();
        }
      }
      store.commit('SET_ROOMCHANGE', true);
      store.commit('SET_SPEAKER', tokenop_oper_appdata.nickname);
      chatDisplay.common_insert(`<span style="color:#da0a0a">${tokenop_oper_appdata.nickname}</span>开始发言`, 1);
    } else {
      store.commit('SET_MICLOADING', false);
      store.commit('SET_MICINDEX', 0);
      store.commit('SET_ROOMLIVE', false);
      store.commit('SET_SPEAKER', '');
      if (loadingTimer) {
        clearInterval(loadingTimer);
        loadingTimer = null;
        store.commit('SET_MICLOADING_TIMER', loadingTimer);
      }
      chat.emitter.trigger(chat.chatEvents.XIAMAI);
    }
  }
}
export function MSG_TOKEN_REQUEST(param, { chat, chatDisplay }) {
  const { packet_object } = param;
  const u = chat.getUser(packet_object.uid);
  if (packet_object.type === 1 && u) {
    chatDisplay.common_insert(`<span style="color:#da0a0a">${u.appdata.nickname}</span>排麦成功`, 1);
  }
}
export function MSG_ROOM_REINIT(param, { store, chat }) {
  const { roompara } = store.getters;
  const reinitParam = JSON.parse(Base64.decode(param.room_reinit_param));
  if (reinitParam.type !== 'main') {
    roompara.relay_switch = false;
    store.commit('SET_MICLOADING', false);
    store.commit('SET_MICINDEX', 0);
    store.commit('SET_ROOMLIVE', false);
    store.commit('SET_SPEAKER', '');
    if (loadingTimer) {
      clearInterval(loadingTimer);
    }
    chat.emitter.trigger(chat.chatEvents.XIAMAI);
  } else {
    roompara.relay_switch = true;
  }
  roompara.parent_room_id = reinitParam.parent_room;
  store.commit('SET_ROOMPARA', roompara);
}
export function MSG_SOCKET_ERROR({ chatDisplay, xmessager }) {
  xmessager.logined = false
  chatDisplay.common_insert('<span style="color:red">聊天服务器已断开连接,请刷新或退出房间重试!</span>', 1);
}
